﻿package com.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.xxx.R;
import com.ToDayModeBean;
import com.FocusFragmentAdapter;
import com.RecyclerMilitaryAdapter;
import com.PopupWindowUtil;
import com.BaseActivity;
import com.LogUtil;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Creator:Yx
 * Time:2020/7/21 11:37
 * Description:军事百科 Military Encyclopedia
 */
public class MilitaryActivity extends BaseActivity {
    private RelativeLayout title_layout;
    private TextView tvLeft, tvTitle, tvRight;
    private ScrollView scrollView;
    //第二部分 悬浮
    private LinearLayout cardll, suspend02, suspend01, suspend_ll;//已显示的，隐藏的，需要添加/删除的布局
    private ScrollView hs;
    private int suspend_H;//第二部分 高度
    private MyHandler handler;
    private int lastY = 0;
    private final int touchEventId = 730;
    private int maxTIME = 1000;//手指抬起后每300毫秒检测一次坐标，最多不能大于1200毫秒
    private int timeCount = 0;
    private TabLayout tabLayout;
    private ImageView imageView;
    private List<String> titleList = new ArrayList<>();
    private RecyclerView recyclerView;
    private RecyclerMilitaryAdapter adapter;

    private int positions = 0;
    private boolean isOpen = false;//是否展开全部导航

    @Override
    public int initLayout() {
        return R.layout.activity_military;
    }

    @Override
    public void initView() {
        title_layout = findViewById(R.id.title_layout);
        tvLeft = findViewById(R.id.tv_left);
        tvLeft.setOnClickListener(clicks);
        tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText(getIntent().getStringExtra("mTitle") + "");
        tvRight = findViewById(R.id.tv_right);
        //TextViewImgUtil.setDrawableLeft(this, tvRight, R.drawable.icon_search3);
        tvRight.setOnClickListener(clicks);


        scrollView = findViewById(R.id.military_scrollview);
        //第二部分，悬浮
        cardll = findViewById(R.id.item_public_card_ll);//要悬浮的布局上面内容布局的高度
        suspend02 = (LinearLayout) findViewById(R.id.suspend02);//被隐藏的
        suspend_ll = (LinearLayout) findViewById(R.id.suspend_ll);//操作的布局
        suspend01 = (LinearLayout) findViewById(R.id.suspend01);//初始显示的
        //导航
        tabLayout = findViewById(R.id.public_tabLayout);
        imageView = findViewById(R.id.public_xiala);//展开所有导航
        imageView.setOnClickListener(clicks);
        //数据
        recyclerView = findViewById(R.id.recyclerview_military);
        recyclerView.setNestedScrollingEnabled(true);//禁止滑动
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false) {//禁止滑动加强
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new RecyclerMilitaryAdapter(this, 0);
        recyclerView.setAdapter(adapter);

        //整个ScorllView滑动监听
        handler = new MyHandler(this);
        inItScorllY();

        titleList.add("无人机");
        titleList.add("人工智能");
        titleList.add("人工智能无人机");
        titleList.add("发动机");
        titleList.add("科普军营");
        titleList.add("科普军营");

        tabLayout.addTab(tabLayout.newTab().setText("无人机"), 0, true);//添加tab指定tab位置并设为选中
        tabLayout.addTab(tabLayout.newTab().setText("人工智能"));
        tabLayout.addTab(tabLayout.newTab().setText("人工智能无人机"));
        tabLayout.addTab(tabLayout.newTab().setText("发动机"));
        tabLayout.addTab(tabLayout.newTab().setText("科普军营"));
        tabLayout.addTab(tabLayout.newTab().setText("科普军营"));
        //设置Tab样式和监听
        initTypeListener();


        List<ToDayModeBean.ToDayMode> mLists = new ArrayList<>();
        ToDayModeBean bean = new ToDayModeBean();
        ToDayModeBean.ToDayMode mode = bean.new ToDayMode();
        mode.setTitle("流言：辟谣病毒通过蔬菜传播");
        mode.setLabel("科普军营");
        mode.setDate("2020-2-2");
        mode.setImage(new String[]{"https://boot-img.xuexi.cn/image/1005/upload/a470c8b475d040339fce2321833e8e22.jpg"});
        mode.setNumberOfComment("66");
        mLists.add(mode);
        bean = new ToDayModeBean();
        ToDayModeBean.ToDayMode mode2 = bean.new ToDayMode();
        mode2.setTitle("辟谣：1234566789");
        mode2.setLabel("科普军营");
        mode2.setDate("2020");
        mode2.setImage(new String[]{"https://boot-img.xuexi.cn/image/1005/upload/a470c8b475d040339fce2321833e8e22.jpg"});
        mode2.setNumberOfComment("66");
        mLists.add(mode2);
        bean = new ToDayModeBean();
        ToDayModeBean.ToDayMode mode3 = bean.new ToDayMode();
        mode3.setTitle("测试测试测试！3333");
        mode3.setLabel("科普军营");
        mode3.setDate("2020");
        mode3.setImage(new String[]{"https://boot-img.xuexi.cn/image/1005/upload/a470c8b475d040339fce2321833e8e22.jpg"});
        mode3.setNumberOfComment("66");
        mLists.add(mode3);
        bean = new ToDayModeBean();
        ToDayModeBean.ToDayMode mode4 = bean.new ToDayMode();
        mode4.setTitle("测试测试测试！3333");
        mode4.setLabel("科普军营");
        mode4.setDate("2020");
        mode4.setImage(new String[]{"https://boot-img.xuexi.cn/image/1005/upload/a470c8b475d040339fce2321833e8e22.jpg"});
        mode4.setNumberOfComment("646");
        mLists.add(mode4);
        bean = new ToDayModeBean();
        ToDayModeBean.ToDayMode mode5 = bean.new ToDayMode();
        mode5.setTitle("测试测试测试！3333");
        mode5.setLabel("科普军营");
        mode5.setDate("2020");
        mode5.setImage(new String[]{"https://boot-img.xuexi.cn/image/1005/upload/a470c8b475d040339fce2321833e8e22.jpg"});
        mode5.setNumberOfComment("6");
        mLists.add(mode5);
        bean = new ToDayModeBean();
        ToDayModeBean.ToDayMode mode6 = bean.new ToDayMode();
        mode6.setTitle("测试测试测试！3333");
        mode6.setLabel("科普军营");
        mode6.setDate("1920");
        mode6.setImage(new String[]{"https://boot-img.xuexi.cn/image/1005/upload/a470c8b475d040339fce2321833e8e22.jpg"});
        mode6.setNumberOfComment("66");
        mLists.add(mode6);
        bean = new ToDayModeBean();
        ToDayModeBean.ToDayMode mode7 = bean.new ToDayMode();
        mode7.setTitle("测试测试测试！3333");
        mode7.setLabel("科普军营");
        mode7.setDate("2020");
        mode7.setImage(new String[]{"https://boot-img.xuexi.cn/image/1005/upload/a470c8b475d040339fce2321833e8e22.jpg"});
        mode7.setNumberOfComment("26");
        mLists.add(mode7);
        bean = new ToDayModeBean();
        ToDayModeBean.ToDayMode mode8 = bean.new ToDayMode();
        mode8.setTitle("测试测试测试！3333");
        mode8.setLabel("科普军营");
        mode8.setDate("2020");
        mode8.setImage(new String[]{"https://boot-img.xuexi.cn/image/1005/upload/a470c8b475d040339fce2321833e8e22.jpg"});
        mode8.setNumberOfComment("26");
        mLists.add(mode8);
        bean = new ToDayModeBean();
        ToDayModeBean.ToDayMode mode9 = bean.new ToDayMode();
        mode9.setTitle("测试测试测试！3333");
        mode9.setLabel("科普军营");
        mode9.setDate("2020");
        mode9.setImage(new String[]{"https://boot-img.xuexi.cn/image/1005/upload/a470c8b475d040339fce2321833e8e22.jpg"});
        mode9.setNumberOfComment("26");
        mLists.add(mode9);
        bean = new ToDayModeBean();
        ToDayModeBean.ToDayMode mode10 = bean.new ToDayMode();
        mode10.setTitle("测试测试测试！3333");
        mode10.setLabel("科普军营");
        mode10.setDate("2020");
        mode10.setImage(new String[]{"https://boot-img.xuexi.cn/image/1005/upload/a470c8b475d040339fce2321833e8e22.jpg"});
        mode10.setNumberOfComment("26");
        mLists.add(mode10);
        bean = new ToDayModeBean();
        ToDayModeBean.ToDayMode mode11 = bean.new ToDayMode();
        mode11.setTitle("测试测试测试！3333");
        mode11.setLabel("科普军营");
        mode11.setDate("2020");
        mode11.setImage(new String[]{"https://boot-img.xuexi.cn/image/1005/upload/a470c8b475d040339fce2321833e8e22.jpg"});
        mode11.setNumberOfComment("26");
        mLists.add(mode11);
        adapter.setData(mLists);

    }


    /**
     * 导航类型
     */
    private void initTypeListener() {
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);//填满一个屏幕 可以左右滑动
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);//充满
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //TextView title = (TextView) (((LinearLayout) ((LinearLayout) tabLayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
                //title.setTextSize(20);
                //title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                if (isOpen) {
                    closePopUpdateImg();
                }
                positions = tab.getPosition();
                selectTab(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //TextView title = (TextView) (((LinearLayout) ((LinearLayout) tabLayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
                //title.setTextSize(20);
                //title.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    private void selectTab(int position) {
        switch (position) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                break;
        }
    }


    /**
     * 最后监听滑动事件
     */
    @SuppressLint("ClickableViewAccessibility")
    private void inItScorllY() {
        scrollView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // 不断的调用handler。给它发消息
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        handler.sendMessageDelayed(handler.obtainMessage(touchEventId, v), 5);
                        break;
                    case MotionEvent.ACTION_MOVE:// 手指按住拖动
                        handler.sendMessageDelayed(handler.obtainMessage(touchEventId, v), 5);
                        break;
                    case MotionEvent.ACTION_UP: // 按下抬起,终止触摸时刻
                        handler.sendMessageDelayed(handler.obtainMessage(touchEventId, v), 5);
                        //假设终止触摸时坐标是大于Layout的高度的，就造成标题栏的颜色依然是有颜色的
                        //所以在手指抬起后的300~1200豪秒时间内再次去检查一下此时的坐标
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                timeCount = timeCount + 200;
                                if (timeCount <= maxTIME) {
                                    handler.sendMessageDelayed(handler.obtainMessage(touchEventId, ""), 5);
                                    new Handler().postDelayed(this, 100);
                                } else {
                                    timeCount = 0;//归零
                                }
                            }
                        }, 100);
                        break;
                }
                return false;
            }
        });


    }

    private class MyHandler extends Handler {
        private WeakReference<MilitaryActivity> weakReference;
//        private HandlerOOMActivity activity;

        public MyHandler(MilitaryActivity activity) {
            weakReference = new WeakReference<>(activity);
//            this.activity = activity;
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//            HandlerOOMActivity activity = weakReference.get();
            switch (msg.what) {
                case touchEventId:
                    lastY = scrollView.getScrollY();
                    LogUtil.iYx("=======mScrollView滑动了:===:" + lastY);//当前滑动距离

                    int topHeight = title_layout.getHeight();//顶部title的高度
                    int maxHeight = cardll.getHeight();//布局1高度，悬浮控件上面的一个布局的高度
                    //LogUtil.iYx("=======maxHeight:===:" + maxHeight);
                    //----------------------控制标题颜色-----------------------------
                    if (lastY <= 0) {
                        title_layout.setBackgroundColor(Color.argb((int) 0, 0, 0, 0));
                    } else if (lastY > 0 && lastY <= topHeight) {
                        float scale = (float) lastY / topHeight;
                        float alpha = (255 * scale);//255最大色值
                        title_layout.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255));

                    } else {
                        title_layout.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
                    }
                    //-----------------------↑end↑----------------------------

                    //--------------------控制系列导航悬浮---------------------------------
                    if (lastY >= maxHeight) {
                        if (suspend_ll.getParent() != suspend01) {
                            suspend02.removeView(suspend_ll);
                            suspend01.addView(suspend_ll);
                        }
                    } else {
                        if (suspend_ll.getParent() != suspend02) {
                            suspend01.removeView(suspend_ll);
                            suspend02.addView(suspend_ll);
                        }
                    }
                    //-----------------------↑end↑-------------------------------
                    break;
                default:
                    break;
            }

        }
    }


    private View.OnClickListener clicks = v -> {
        switch (v.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right:
                //presenter.setGridTabClicks(this, 10, "搜索");
                break;
            case R.id.public_xiala:
                if (isOpen) {
                    closePopUpdateImg();
                } else {
                    isOpen = true;
                    imageView.setBackground(ContextCompat.getDrawable(this, R.drawable.icon_black_up2));
                    PopupWindowUtil.showTab(this, imageView, titleList, positions, new PopupWindowUtil.onYxCallback() {
                        @Override
                        public void onSuccess(String result) {
                            isOpen = false;
                            imageView.setBackground(ContextCompat.getDrawable(MilitaryActivity.this, R.drawable.icon_black_open2));
                            try {
                                int position = Integer.parseInt(result);
                                tabLayout.getTabAt(position).select();
                            } catch (Exception e) {

                            }
                        }

                        @Override
                        public void onFail() {
                            isOpen = false;
                            imageView.setBackground(ContextCompat.getDrawable(MilitaryActivity.this, R.drawable.icon_black_open2));
                        }
                    });
                }

                break;
        }
    };

    private void closePopUpdateImg() {
        isOpen = false;
        PopupWindowUtil.hindPopupWindow2();
        imageView.setBackground(ContextCompat.getDrawable(this, R.drawable.icon_black_open2));
    }
}
